<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员姓名" prop="membersName">
        <el-input v-model="queryParams.membersName" placeholder="请输入会员姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="会员工号" prop="membersNum">
        <el-input type="number" min="0" v-model="queryParams.membersNum" placeholder="请输入会员工号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="membersTel">
        <el-input type="number" min="0" v-model="queryParams.membersTel" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所在校区" prop="school">
        <el-input v-model="queryParams.school" placeholder="请输入所在校区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['members:management:edit']">修改</el-button>
      </el-col>
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="membersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="membersName" />
      <el-table-column label="工号" align="center" prop="membersNum" width="100" />
      <el-table-column label="性别" align="center" prop="membersSex" width="60">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.membersSex" />
        </template>
      </el-table-column>
      <el-table-column label="个人照片" align="center" prop="membersImg">
        <template slot-scope="scope">
          <el-image :src="scope.row.membersImg" style="width: 100px; height: 80px"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="membersBir" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.membersBir, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="membersTel" width="120" />
      <el-table-column label="学历" align="center" prop="membersBackground" width="80" />
      <el-table-column label="民族" align="center" prop="membersNation" width="80" />
      <el-table-column label="来校时间" align="center" prop="membersSchoolDate" width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.membersSchoolDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="婚姻状况" align="center" prop="membersMaritalStatus" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_marital" :value="scope.row.membersMaritalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="籍贯" align="center" prop="membersNativePlace" />
      <el-table-column label="家庭住址" align="center" prop="membersAddress" width="180" />
      <el-table-column label="所在校区" align="center" prop="school" width="130" />
      <el-table-column label="用人方式" align="center" prop="membersModeEmployment" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_mode_employment" :value="scope.row.membersModeEmployment" />
        </template>
      </el-table-column>
      <el-table-column label="人员状态" align="center" prop="membersAdmissionTime" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_members_status" :value="scope.row.membersAdmissionTime" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="90">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['members:management:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="姓名" prop="membersName">
          <el-input v-model="form.membersName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="工号" prop="membersNum">
          <el-input type="number" min="0" v-model="form.membersNum" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="性别" prop="membersSex">
          <el-select v-model="form.membersSex" placeholder="请选择性别">
            <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="membersTel">
          <el-input type="number" v-model="form.membersTel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="所在校区" prop="school">
          <el-input v-model="form.school" placeholder="请输入所在校区" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  
</template>

<script>
import { listMembers, getMembers, delMembers, updateMembers } from "@/api/members/management";
export default {
  name: "MembersManagement",
  dicts: ["sys_user_sex", "sys_marital", "sys_mode_employment", "sys_members_status"],
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      membersList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        membersName: undefined,
        membersNum: undefined,
        membersTel: undefined,
        school: undefined
      },
      form: {},
      rules: {
        membersName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        membersNum: [{ required: true, message: "工号不能为空", trigger: "blur" }],
        membersSex: [{ required: true, message: "性别不能为空", trigger: "change" }],
        membersTel: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
        school: [{ required: true, message: "所在校区不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listMembers(this.queryParams).then(res => {
        this.membersList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        membersId: undefined,
        membersName: undefined,
        membersNum: undefined,
        membersSex: undefined,
        membersTel: undefined,
        school: undefined
      };
      this.resetForm && this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm && this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.membersId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleUpdate(row) {
      this.reset();
      const membersId = row.membersId || this.ids;
      getMembers(membersId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员";
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateMembers(this.form).then(() => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    handleDelete(row) {
      const membersIds = row.membersId || this.ids;
      this.$modal
        .confirm('是否确认删除会员编号为"' + membersIds + '"的数据项？')
        .then(function () {
          return delMembers(membersIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>


